﻿Imports System.Drawing
Imports System.Windows.Forms

Public Class FormList

    Public Event OnInsert()
    Public Event OnModify()
    Public Event OnDelete()
    Public Event OnSearch()
    Public Shadows Event OnPrint()
    Public Event OnClose()
    Public Event OnCustomButtonCreating(ByRef listCustomButton As List(Of Button))
    Public Event OnFilterSearch(ByVal inText As String)

    Public Property NameList As String
        Get
            Return lblNameList.Text
        End Get
        Set(ByVal value As String)
            lblNameList.Text = value
        End Set
    End Property
    Public Property NameForm As String
        Get
            Return mNameForm
        End Get
        Set(ByVal value As String)
            mNameForm = value
        End Set
    End Property
    Public Property PicCommandsWidth As Integer
        Get
            Return picCommands.Width
        End Get
        Set(ByVal value As Integer)
            picCommands.Width = value
        End Set
    End Property

    Public Property ImageBarTop As Image
        Get
            Return picHeader.Image
        End Get
        Set(ByVal value As Image)
            picHeader.Image = value
        End Set
    End Property

    Private mNameForm As String
    Private intCounterTop As Integer = 8
    Private intCounterTabIndex As Integer = 0
    Private listCustomButton As New List(Of Button)

    Public Enum TypeButton
        INSERT
        MODIFY
        DELETE
        SEARCH
        PRINT
        CUSTOM
        SEPARATOR
        CLOSE
    End Enum

    Class InfoButton
        Public intTypeButton As TypeButton
        Public strCustomNameButton As String
        Public imgImage As Image

        Public Sub New(ByVal inTypeButton As TypeButton, Optional ByVal inCustomNameButton As String = "", Optional ByVal inImage As Image = Nothing)
            intTypeButton = inTypeButton
            strCustomNameButton = inCustomNameButton
            imgImage = inImage
        End Sub

    End Class

#Region "Public methods"

    Public Sub AddButtonStandard()
        Try
            AddButton(TypeButton.INSERT)
            AddButton(TypeButton.MODIFY)
            AddButton(TypeButton.DELETE)
            AddButton(TypeButton.SEPARATOR)
            AddButton(TypeButton.SEARCH)
            AddButton(TypeButton.PRINT)
            AddButton(TypeButton.SEPARATOR)
            AddButton(TypeButton.CLOSE)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub AddButton(ByRef inListMenu As List(Of InfoButton))
        Try
            For Each lInfoMenu As InfoButton In inListMenu
                Call AddButton(lInfoMenu.intTypeButton, lInfoMenu.strCustomNameButton, lInfoMenu.imgImage)
            Next
            RaiseEvent OnCustomButtonCreating(listCustomButton)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

#End Region

#Region "Private methods"

    Private Sub AddButton(ByVal inTypeButton As TypeButton, Optional ByVal inCustomNameMenu As String = "", Optional ByVal inCustomPicture As System.Drawing.Image = Nothing)
        Try
            Dim lNewButton As New Base.SuperCommanButton
            Select Case inTypeButton
                Case TypeButton.INSERT
                    lNewButton.Name = "mnuInsert"
                    lNewButton.Text = "     &Insert"
                    lNewButton.Image = My.Resources.imgNew32
                    AddHandler lNewButton.Click, AddressOf Insert
                Case TypeButton.MODIFY
                    lNewButton.Name = "mnuModify"
                    lNewButton.Text = "     &Modify"
                    lNewButton.Image = My.Resources.imgModify32
                    AddHandler lNewButton.Click, AddressOf Modify
                Case TypeButton.DELETE
                    lNewButton.Name = "mnuDelete"
                    lNewButton.Text = "     &Delete"
                    lNewButton.Image = My.Resources.imgDelete32
                    AddHandler lNewButton.Click, AddressOf Delete
                Case TypeButton.SEARCH
                    lNewButton.Name = "mnuSearch"
                    lNewButton.Text = "     &Search"
                    lNewButton.Image = My.Resources.imgSearch32
                    AddHandler lNewButton.Click, AddressOf Search
                Case TypeButton.PRINT
                    lNewButton.Name = "mnuPrint"
                    lNewButton.Text = "     &Print"
                    lNewButton.Image = My.Resources.imgPrint32
                    AddHandler lNewButton.Click, AddressOf Print
                Case TypeButton.CUSTOM
                    lNewButton.Name = "mnuCustom" & intCounterTop
                    lNewButton.Text = "     " & inCustomNameMenu
                    lNewButton.Image = inCustomPicture
                    listCustomButton.Add(lNewButton)
                Case TypeButton.CLOSE
                    lNewButton.Name = "mnuClose"
                    lNewButton.Text = "     &Close"
                    lNewButton.Image = My.Resources.imgClose32
                    AddHandler lNewButton.Click, AddressOf Close
            End Select

            If inTypeButton = TypeButton.SEPARATOR Then
                intCounterTop += 8
            Else
                lNewButton.TabIndex = intCounterTabIndex
                lNewButton.BackColor = System.Windows.Forms.Button.DefaultBackColor
                lNewButton.Left = 14
                lNewButton.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Bold)
                lNewButton.ImageAlign = ContentAlignment.MiddleLeft
                lNewButton.Size = New Size(124, 41)
                lNewButton.Anchor = AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Right
                lNewButton.Top = intCounterTop
                intCounterTop += 5 + lNewButton.Height
                intCounterTabIndex += 1
                picCommands.Controls.Add(lNewButton)
            End If
            lNewButton = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

#End Region

#Region "Events"

    Private Sub Insert()
        Try
            RaiseEvent OnInsert()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Modify()
        Try
            RaiseEvent OnModify()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Delete()
        Try
            RaiseEvent OnDelete()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Search()
        Try
            RaiseEvent OnSearch()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Print()
        Try
            RaiseEvent OnPrint()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Close()
        Try
            RaiseEvent OnClose()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub txtFilterSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFilterSearch.Click
        RaiseEvent OnFilterSearch(txtFilterSearch.Text)
    End Sub

    Private Sub txtFilterSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFilterSearch.TextChanged
        RaiseEvent OnFilterSearch(txtFilterSearch.Text)
    End Sub

#End Region

#Region "Form events"

    Private Sub FormList_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblNameList.Parent = picList
        panSearch.Parent = picList
    End Sub

#End Region

#Region "Paint nome form"

    Private Sub picHeader_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picHeader.Paint
        Try
            Call Print3D(e.Graphics, NameForm, New Point(60, 10), Color.Black, New Font("Microsoft Sans Serif", 18, FontStyle.Bold))
        Catch ex As Exception
            Base.ShowError.Show("Apertura form", Me.Name, "picHeader_Paint", ex)
        End Try
    End Sub

    Private Sub Print3D(ByVal g As Graphics, ByVal text As String, ByVal coords As Point, ByVal col As Color, ByVal fnt As Font)
        Print3D(g, text, coords, col, fnt, Color.DimGray, 1.5, 1.5)
    End Sub

    Private Sub Print3D(ByVal g As Graphics, ByVal text As String, ByVal coords As Point, _
      ByVal col As Color, ByVal fnt As Font, ByVal shadowCol As Color, _
      ByVal shadowOffsetX As Single, ByVal shadowOffsetY As Single)
        ' create the brush for the shadow text
        Dim sbrush As New SolidBrush(shadowCol)
        ' draw the shadow text
        g.DrawString(text, fnt, sbrush, coords.X + shadowOffsetX, _
          coords.Y + shadowOffsetY)
        sbrush.Dispose()

        ' create the brush for the foreground text
        Dim fbrush As New SolidBrush(col)
        ' draw the foreground text
        g.DrawString(text, fnt, fbrush, coords.X, coords.Y)
        fbrush.Dispose()
    End Sub

#End Region

End Class
